package protocol.JT808_2019;

import java.util.HashMap;

public class Handle808_2019JT07 implements IHandle808_2019JT {

    @Override
    public String handle(JT808_2019Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle0700(cmd);
            case 0x01:
                return handle0701(cmd);
            case 0x02:
                return handle0702(cmd);
            case 0x04:
                return handle0704(cmd);
            case 0x05:
                return handle0705(cmd);
            default:
                return "808-2019:未匹配的命令字"+(cmd.readOrder()+","+cmd.subOrder());
        }
    }

    private String handle0705(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:CAN总线数据上传");
        builder.append("\r\n");

        int size = cmd.readWORD();
        builder.append("数据数量："+size);
        builder.append("\r\n");

        builder.append("CAN总线数据接收时间："+cmd.readBCD(5));
        builder.append("\r\n");

        for (int i = 0; i < size; i++) {
            builder.append("CAN ID:"+cmd.readDWORD());
            builder.append("\r\n");

            builder.append("CAN DATA:"+cmd.readBCD(8));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle0704(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:定位数据批量上传");
        builder.append("\r\n");

        int size = cmd.readWORD();
        builder.append("数据数量："+size);
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"正常位置信息");
        list.put(1,"盲区补报");
        int res = cmd.readInt(1);
        builder.append("数据类型（"+res+"):"+list.getOrDefault(res,"未定义"));
        builder.append("\r\n");

        for (int i = 0; i < size; i++) {
            int len = cmd.readWORD();
            builder.append(cmd.readBCD(len));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle0702(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:行驶记录数据上传");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(1,"从业资格证IC卡插入（驾驶员上班）");
        list.put(2,"从业资格证IC卡拔出（驾驶员下班）");
        int res = cmd.readInt(1);
        builder.append("状态（"+res+"):"+list.getOrDefault(res,"未定义"));
        builder.append("\r\n");

        builder.append("时间："+cmd.readBCD(6));
        builder.append("\r\n");

        list.clear();
        list.put(0,"IC卡读卡成功");
        list.put(1,"IC卡读卡失败：卡片密钥认证未通过");
        list.put(2,"IC卡读卡失败：卡片已被锁定");
        list.put(3,"IC卡读卡失败：卡片已被拔出");
        list.put(4,"IC卡读卡失败：数据校验失败");
        int value = cmd.readInt(1);
        builder.append("状态（"+value+"):"+list.getOrDefault(value,"未定义"));
        builder.append("\r\n");

        if (value == 0){
            builder.append("驾驶员姓名："+cmd.readString(cmd.readInt()));
            builder.append("\r\n");
            builder.append("驾驶员编码："+cmd.readString(20));
            builder.append("\r\n");
            builder.append("发证机构："+cmd.readString(cmd.readInt()));
            builder.append("\r\n");
            builder.append("证件有效期："+cmd.readBCD(4));
            builder.append("\r\n");
            builder.append("驾驶员身份证号："+cmd.readString(20));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle0701(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:行驶记录数据上传");
        builder.append("\r\n");
        int size = cmd.readDWORD();
        builder.append("电子运单长度："+size);
        builder.append("\r\n");
        builder.append("电子运单内容："+cmd.readBCD(size));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0700(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:行驶记录数据上传");
        builder.append("\r\n");
        builder.append("应答流水号："+cmd.readBCD(2));
        builder.append("\r\n");
        builder.append("命令字："+cmd.readBCD(1));
        builder.append("\r\n");

        builder.append("数据："+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");
        return builder.toString();
    }
}
